<template>
  <div class="tenantManagement-container">

    <el-collapse-transition>
      <div v-show="this.moreQueryFlag" class="more-query">
        <!-- 更多查找 -->
        <vab-query-form>
          <vab-query-form-left-panel :span="24">
            <el-form :inline="true" :model="queryForm" @submit.native.prevent>

              <el-form-item>
                <el-input
                  v-model.trim="queryForm.goodId_EQ"
                  clearable
                  placeholder="请输入商品id"
                />
              </el-form-item>

              <el-form-item>
                <el-input
                  v-model.trim="queryForm.price_EQ"
                  clearable
                  placeholder="请输入金额"
                />
              </el-form-item>

              <el-form-item>
                <el-select v-model="queryForm.status_EQ" clearable placeholder="请选择订单状态" style="width: 100%">
                  <el-option
                    v-for="item in dict.user_order_status"
                    :key="item.dictValue"
                    :label="item.dictName"
                    :value="item.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-input
                  v-model.trim="queryForm.remarks_LIKE"
                  clearable
                  placeholder="请输入备注"
                />
              </el-form-item>


            </el-form>
          </vab-query-form-left-panel>

        </vab-query-form>
        <el-divider></el-divider>
      </div>
    </el-collapse-transition>

    <!-- 主要操作  -->
    <vab-query-form>
      <vab-query-form-left-panel :span="10">
        <el-button
          v-if="$perms('shop_userorder_insert')"
          icon="el-icon-plus"
          type="primary"
          @click="handleInsert"
        > 添加
        </el-button>

        <el-button
          v-if="$perms('shop_userorder_import')"
          icon="el-icon-upload2"
          type="warning"
          @click="handleImportExcel"
        > 导入
        </el-button>

        <el-button
          v-if="$perms('shop_userorder_export')"
          icon="el-icon-download"
          type="warning"
          @click="handleExportExcel"
        > 导出
        </el-button>

        <el-button
          v-if="$perms('shop_userorder_delete')"
          :disabled="!selectRows.length > 0"
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete"
        > 批量删除
        </el-button>

      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="14">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>

          <el-form-item>
            <el-input
              v-model.trim="queryForm.userId_EQ"
              clearable
              placeholder="请输入用户id"
            />
          </el-form-item>

          <el-form-item>
            <el-select v-model="queryForm.type_EQ" clearable placeholder="请选择订单类型" style="width: 100%">
              <el-option
                v-for="item in dict.user_order_type"
                :key="item.dictValue"
                :label="item.dictName"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>

            <el-button icon="el-icon-search" @click="moreQuery">
              更多
            </el-button>

          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>

      <el-table-column label="预约时间" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.resStartTime }}到{{ scope.row.resEndTime.split(' ')[1] }}
        </template>
      </el-table-column>

      <el-table-column
        label="用户id"
        prop="userId"
        show-overflow-tooltip
        width="95"
      ></el-table-column>

      <el-table-column
        label="订单类型"
        prop="type"
        show-overflow-tooltip
      >

        <template slot-scope="scope">
          <span>
            {{ $getDictNameByValue('user_order_type', scope.row.type) }}
          </span>
        </template>

      </el-table-column>

      <el-table-column
        label="商品id"
        prop="goodId"
        show-overflow-tooltip
      ></el-table-column>

      <el-table-column
        label="金额"
        prop="price"
        show-overflow-tooltip
      ></el-table-column>

      <el-table-column
        label="订单状态"
        prop="status"
        show-overflow-tooltip
      >

        <template slot-scope="scope">
          <span>
            {{ $getDictNameByValue('user_order_status', scope.row.status) }}
          </span>
        </template>

      </el-table-column>

      <el-table-column
        label="备注"
        prop="remarks"
        show-overflow-tooltip
      ></el-table-column>


      <el-table-column
        v-if="$perms('shop_userorder_update') || $perms('shop_userorder_delete')"
        label="操作"
        show-overflow-tooltip
        width="200"
      >
        <template v-slot="scope">
          <el-button
            v-if="$perms('shop_userorder_update')"
            type="text"
            @click="handleUpdate(scope.row)"
          > 编辑
          </el-button>

          <el-divider direction="vertical"></el-divider>

          <el-button
            v-if="$perms('shop_userorder_delete')"
            type="text"
            @click="handleDelete(scope.row)"
          > 删除
          </el-button>
        </template>

      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>

    <edit ref="edit" @fetchData="fetchData"></edit>
    <import ref="import" @fetchData="fetchData"></import>

  </div>
</template>

<script>
import {doDelete, doDeleteAll, doExportExcel, getList} from "@/api/shop/userorder/SysUserOrderManagementApi";
import Edit from "./components/SysUserOrderManagementEdit";
import Import from "./components/SysUserOrderManagementImport";

import {vueButtonClickBan} from "@/utils";
import {isNotNull} from "@/utils/valiargs";

export default {
  name: "SysUserOrderManagement",
  components: {Edit, Import},
  data() {
    return {
      list: null,
      listLoading: true,
      layout: "total, prev, pager, next, sizes, jumper",
      total: 0,
      selectRows: "",
      elementLoadingText: "正在加载...",
      moreQueryFlag: false,
      queryForm: {
        pageNo: 1,
        pageSize: 10,
        userId_EQ: "",
        type_EQ: "",
        goodId_EQ: "",
        price_EQ: "",
        status_EQ: "",
        remarks_LIKE: "",
        resStartTime_ORDER: "DESC"
      },
      dict: {},
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    this.fetchData();
  },
  mounted() {
    this.dict.user_order_type = this.$getDictList("user_order_type");
    this.dict.user_order_status = this.$getDictList("user_order_status");
  },
  methods: {
    setSelectRows(val) {
      this.selectRows = val;
    },
    handleInsert(row) {
      this.$refs["edit"].showEdit();
    },
    handleUpdate(row) {
      if (row.id) {
        this.$refs["edit"].showEdit(row);
      }
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm("你确定要删除当前项吗", null, async () => {
          const {msg} = await doDelete({id: row.id});
          this.$baseMessage(msg, "success");
          await this.fetchData();
        });
      } else {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id).join();
          this.$baseConfirm("你确定要删除选中项吗", null, async () => {
            const {msg} = await doDeleteAll({ids});
            this.$baseMessage(msg, "success");
            await this.fetchData();
          });
        } else {
          this.$baseMessage("未选中任何行", "error");
          return false;
        }
      }
    },
    // 导出excel
    handleExportExcel(el) {
      // 导出按钮防抖处理 默认限制为10秒
      vueButtonClickBan(el, 10);

      // 执行导出
      doExportExcel(this.queryForm);
    },
    // 导入excel
    handleImportExcel() {
      this.$refs["import"].show();
    },


    handleSizeChange(val) {
      this.queryForm.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val;
      this.fetchData();
    },
    moreQuery() {
      this.moreQueryFlag = !this.moreQueryFlag;
    },
    queryData() {


      this.queryForm.pageNo = 1;
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const {data} = await getList(this.queryForm);
      if (isNotNull(data)) {
        this.list = data.rows;
        this.total = data.total;
      }
      setTimeout(() => {
        this.listLoading = false;
      }, 300);
    },
  },
};
</script>
